<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问卷名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入问卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:questionnaire:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:questionnaire:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:questionnaire:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:questionnaire:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionnaireList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="问卷ID" align="center" prop="id" />
      <el-table-column label="问卷名称" align="center" prop="name" />
<!--      <el-table-column label="问卷封面图片地址" align="center" prop="logoPicUrl" />-->

      <el-table-column label="问题0题目" align="center" prop="question0Name" />
      <el-table-column label="问题1题目" align="center" prop="question1Name" />
      <el-table-column label="问题2题目" align="center" prop="question2Name" />
      <el-table-column label="问题3题目" align="center" prop="question3Name" />
      <el-table-column label="问题4题目" align="center" prop="question4Name" />
      <el-table-column label="问题5题目" align="center" prop="question5Name" />
      <el-table-column label="问题6题目" align="center" prop="question6Name" />
      <el-table-column label="问题7题目" align="center" prop="question7Name" />
      <el-table-column label="问题8题目" align="center" prop="question8Name" />
      <el-table-column label="问题9题目" align="center" prop="question9Name" />
      <el-table-column label="问题10题目" align="center" prop="question10Name" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:questionnaire:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:questionnaire:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goWen(scope.row)"
          >浏览问卷</el-button>


          <router-link :to="{ path: '/show', query: { id: scope.row.id } }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goShow(scope.row)"
          >数据统计</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问卷信息主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入问卷名称" />
        </el-form-item>
        <el-form-item label="问卷封面图片地址" prop="logoPicUrl">
<!--          <el-input v-model="form.logoPicUrl" placeholder="请输入问卷封面图片地址" />-->
          <image-upload :value="form.logoPicUrl"></image-upload>
        </el-form-item>
        <el-form-item label="问题0题目" prop="question0Name">
          <el-input v-model="form.question0Name" placeholder="请输入问题0题目" />
        </el-form-item>

        <el-form-item label="问题1题目" prop="question1Name">
          <el-input v-model="form.question1Name" placeholder="请输入问题1题目" />
        </el-form-item>

        <el-form-item label="问题2题目" prop="question2Name">
          <el-input v-model="form.question2Name" placeholder="请输入问题2题目" />
        </el-form-item>

        <el-form-item label="问题3题目" prop="question3Name">
          <el-input v-model="form.question3Name" placeholder="请输入问题3题目" />
        </el-form-item>
        <el-form-item label="问题4题目" prop="question4Name">
          <el-input v-model="form.question4Name" placeholder="请输入问题4题目" />
        </el-form-item>

        <el-form-item label="问题5题目" prop="question5Name">
          <el-input v-model="form.question5Name" placeholder="请输入问题5题目" />
        </el-form-item>

        <el-form-item label="问题6题目" prop="question6Name">
          <el-input v-model="form.question6Name" placeholder="请输入问题6题目" />
        </el-form-item>
        <el-form-item label="问题7题目" prop="question7Name">
          <el-input v-model="form.question7Name" placeholder="请输入问题7题目" />
        </el-form-item>
        <el-form-item label="问题8题目" prop="question8Name">
          <el-input v-model="form.question8Name" placeholder="请输入问题8题目" />
        </el-form-item>
        <el-form-item label="问题9题目" prop="question9Name">
          <el-input v-model="form.question9Name" placeholder="请输入问题9题目" />
        </el-form-item>
        <el-form-item label="问题10题目" prop="question10Name">
          <el-input v-model="form.question10Name" placeholder="请输入问题10题目" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestionnaire, getQuestionnaire, delQuestionnaire, addQuestionnaire, updateQuestionnaire } from "@/api/system/questionnaire";


export default {
  name: "Questionnaire",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷信息主表格数据
      questionnaireList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        logoPicUrl: null,
        question0Name: null,
        question0Type: null,
        question0TypeId: null,
        question1Name: null,
        question1Type: null,
        question1TypeId: null,
        question2Name: null,
        question2Type: null,
        question2TypeId: null,
        question3Name: null,
        question3Type: null,
        question3TypeId: null,
        question4Name: null,
        question4Type: null,
        question4TypeId: null,
        question5Name: null,
        question5Type: null,
        question5TypeId: null,
        question6Name: null,
        question6Type: null,
        question6TypeId: null,
        question7Name: null,
        question7Type: null,
        question7TypeId: null,
        question8Name: null,
        question8Type: null,
        question8TypeId: null,
        question9Name: null,
        question9Type: null,
        question9TypeId: null,
        question10Name: null,
        question10Type: null,
        question10TypeId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "问卷名称不能为空", trigger: "blur" }
        ],
        logoPicUrl: [
          { required: true, message: "问卷封面图片地址不能为空", trigger: "blur" }
        ],
        question0Name: [
          { required: true, message: "问题0题目不能为空", trigger: "blur" }
        ],
        question1Name: [
          { required: true, message: "问题1题目不能为空", trigger: "blur" }
        ],
        question2Name: [
          { required: true, message: "问题2题目不能为空", trigger: "blur" }
        ],
        question3Name: [
          { required: true, message: "问题3题目不能为空", trigger: "blur" }
        ],
        question4Name: [
          { required: true, message: "问题4题目不能为空", trigger: "blur" }
        ],
        question5Name: [
          { required: true, message: "问题5题目不能为空", trigger: "blur" }
        ],
        question6Name: [
          { required: true, message: "问题6题目不能为空", trigger: "blur" }
        ],
        question7Name: [
          { required: true, message: "问题7题目不能为空", trigger: "blur" }
        ],
        question8Name: [
          { required: true, message: "问题8题目不能为空", trigger: "blur" }
        ],
        question9Name: [
          { required: true, message: "问题9题目不能为空", trigger: "blur" }
        ],
        question10Name: [
          { required: true, message: "问题10题目不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goShow(row){

    },
    goWen(row){
        const h5PageUrl ="http://localhost:63342/kfdsks10/ruoyi/ruoyi-ui/src/question-h5/question.h5.html";
        window.location.href = `${h5PageUrl}?id=${row.id}`;
    },
    /** 查询问卷信息主列表 */
    getList() {
      this.loading = true;
      listQuestionnaire(this.queryParams).then(response => {
        this.questionnaireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        logoPicUrl: null,
        question0Name: null,
        question0Type: null,
        question0TypeId: null,
        question1Name: null,
        question1Type: null,
        question1TypeId: null,
        question2Name: null,
        question2Type: null,
        question2TypeId: null,
        question3Name: null,
        question3Type: null,
        question3TypeId: null,
        question4Name: null,
        question4Type: null,
        question4TypeId: null,
        question5Name: null,
        question5Type: null,
        question5TypeId: null,
        question6Name: null,
        question6Type: null,
        question6TypeId: null,
        question7Name: null,
        question7Type: null,
        question7TypeId: null,
        question8Name: null,
        question8Type: null,
        question8TypeId: null,
        question9Name: null,
        question9Type: null,
        question9TypeId: null,
        question10Name: null,
        question10Type: null,
        question10TypeId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问卷信息主";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestionnaire(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问卷信息主";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateQuestionnaire(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestionnaire(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问卷信息主编号为"' + ids + '"的数据项？').then(function() {
        return delQuestionnaire(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/questionnaire/export', {
        ...this.queryParams
      }, `questionnaire_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
